<template>
  <div class="user-container">
    <!-- 面包屑导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>-->
    <bread :second="'用户管理'" :third="'用户列表'"></bread>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div style="margin-top: 15px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="userObj.query" class="iptBox">
              <el-button slot="append" icon="el-icon-search" @click="getUsers()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="addUsers">添加用户</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" border="" style="width: 100%;margin:20px 0">
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <!-- ==={{scope.row.id}}==={{scope.row.mg_state}} -->
            <el-switch
              @change="changeUserState(scope.row)"
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
            ></el-button>
            <el-button icon="el-icon-share" size="mini" @click="handleRoles(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userObj.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="userObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑用户的表单对话框 -->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
      <el-form ref="formEditDialog" :model="formEdit" :rules="rulesFormEdit">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input :disabled="true" v-model="formEdit.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="formEdit.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
          <el-input v-model="formEdit.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加用户的表单对话框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisibleAdd">
      <el-form ref="formEditDialogAdd" :model="formAdd" :rules="rulesFormAdd">
        <el-form-item prop="username" label="用户名" :label-width="formLabelWidth">
          <el-input v-model="formAdd.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-width="formLabelWidth">
          <el-input v-model="formAdd.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="formAdd.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
          <el-input v-model="formAdd.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加用户的表单对话框 -->
    <el-dialog title="分配角色" :visible.sync="dialogFormVisibleRoles">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="当前的用户：">{{objRoles.username}}</el-form-item>
        <el-form-item label="当前的角色：">{{objRoles.role_name}}</el-form-item>
        <el-form-item label="分配新角色：">
          {{form.rid}}
          <el-select v-model="form.rid" placeholder="请选择活动区域">
            <el-option
              v-for="(item,index) in  roleList"
              :key="index"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRoles = false">取 消</el-button>
        <el-button type="primary" @click="rolesFormSet">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>        
<script>
import { users_api, usersState_api, usersDelete_api, usersEdit_api, usersAdd_api, rolesList_api, rolesSet_api } from '@/api'
export default {
  data() {
    return {
      tableData: [],
      total: 1,

      /* 分页中的当前页码数 */

      /* 获取数据列表参数对象 */
      userObj: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },

      /* 对话框数据 */
      dialogFormVisible: false,
      formEdit: {},
      formLabelWidth: '120px',
      //  表单校验规则
      rulesFormEdit: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
          //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      },

      /* 添加用户对话框 */
      formAdd: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      dialogFormVisibleAdd: false,
      //  表单校验规则
      rulesFormAdd: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      },

      /* 分配角色 */
      dialogFormVisibleRoles: false,
      form: {
        rid: '',
        id: JSON.parse(window.sessionStorage.getItem('user')).id
      },
      objRoles: {},
      roleList: []
    }
  },
  created() {
    this.getUsers()
  },
  methods: {
    /* 分页组件提供的事件 */
    handleSizeChange(val) {
      /*  console.log(`每页 ${val} 条`) */
      this.userObj.pagesize = val
      this.getUsers()
    },
    handleCurrentChange(val) {
      /*   console.log(`当前页: ${val}`)
   将获取的当前页赋值给  pagenum */
      this.userObj.pagenum = val
      this.getUsers()
    },

    //  操作中的按钮
    handleEdit(row) {
      // console.log(row, 6677)
      this.formEdit = row
      this.dialogFormVisible = true
    },
    /* 点击编辑按钮中的确认按钮 */
    editForm() {
      this.$refs.formEditDialog.validate(async vaide => {
        //console.log(vaide,5555556666);
        if (vaide) {
          console.log(this.formEdit, 8899)
          const { data: res } = await usersEdit_api(this.formEdit)
          this.dialogFormVisible = false
          //  重新渲染最新表格数据
          this.getUsers()
        } else {
          return
        }
      })
    },
    /* 删除用户 */
    async handleDelete(id) {
      //console.log(id, 444)
      const res2 = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(res2, 88899)
      if (res2 == 'confirm') {
        const { data: res } = await usersDelete_api(id)
        // console.log(res, 4455)

        //  重新渲染最新表格数据
        this.getUsers()
      }

      //   this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(async result => {
      //       console.log(result,123);
      //       /* 这里的result获取的是点击确认按钮的值，然后通过判断返回值来警醒是否发生请求 */
      //       if(result=='confirm'){
      //         const {data:res} = await usersDelete_api(id)
      //         console.log(res,4455);

      //         //  重新渲染最新表格数据
      //         this.getUsers()
      //       }
      //     })
      //     .catch(() => {
      //       this.$message({
      //         type: 'info',
      //         message: '已取消删除'
      //       })
      //     })
    },

    /* 添加用户 */
    addUsers() {
      this.dialogFormVisibleAdd = true
    },
    /* 添加用户点击确认按钮 */
    addForm() {
      this.$refs.formEditDialogAdd.validate(async valide => {
        if (valide) {
          const { data: res } = await usersAdd_api(this.formAdd)
          this.dialogFormVisibleAdd = false
          //  重新渲染最新表格数据
          this.getUsers()

          // 清空表单数据
          this.$refs.formEditDialogAdd.resetFields()
        } else {
          return
        }
      })
    },

    /* 获取用户数据列表 */
    async getUsers() {
      const { data: res } = await users_api(this.userObj)
      console.log(res, 888)
      //  将获取的用户数据赋值给tableData
      this.tableData = res.data.users
      this.total = res.data.total
    },

    /* 更新用户状态 */
    async changeUserState(row) {
      //console.log(row,44444);
      // usersState_api({uId:1,type:1})
      const { data: res } = await usersState_api({
        uId: row.id,
        type: row.mg_state
      })
      console.log(res, 555)
    },

    /* 分配角色===获取所有的角色 */
    async handleRoles(row) {
      this.dialogFormVisibleRoles = true
      this.objRoles = row
      // console.log(this.objRoles, 666666)
      const { data: res } = await rolesList_api()
      //console.log(res,45);
      this.roleList = res.data
    },

    //  分配角色
    async rolesFormSet() {
      const { data: res } = await rolesSet_api(this.form)
      console.log(res, 4455)
      this.dialogFormVisibleRoles = false
      this.getUsers()
    }
  }
}
</script>
<style scoped lang='less'>
.user-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .box-card {
    .iptBox {
    }
  }
}
</style>